<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">分类</label>
                        <div class="layui-input-block">
                            <div name="catid" id="catid" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" placeholder="" lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">图标</label>
                        <div class="layui-input-block">
                            <img class="img-3" src=""/>
                            <input type="text" style="display:none" name="image" value="" lay-verify="required"/>
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="image"  permission="app.admin.upload.image">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="attachment-choose-image_path"  permission="app.admin.upload.attachment">
                                <i class="layui-icon layui-icon-align-left"></i>选择图片
                            </button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">显示时长</label>
                        <div class="layui-input-block">
                            <input type="number" name="show_tts" placeholder="" lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签</label>
                        <div class="layui-input-block">
                            <input type="text" name="tag" placeholder="" class="layui-input">
                            <tip style="color: red;font-size: 12px;">多个用英文逗号`,`隔开</tip>
                        </div>
                    </div>
                    <!--<div class="layui-form-item">-->
                        <!--<label class="layui-form-label required" >素材视频</label>-->
                        <!--<div class="layui-input-block" >-->
                            <!--<a type="button" class="layui-btn layui-btn-primary layui-btn-radius" id="video_upload" path="material">-->
                                <!--<i class="layui-icon">&#xe624;</i>上传视频-->
                            <!--</a>-->
                            <!--<div class="video_html_append"></div>-->
                        <!--</div>-->
                    <!--</div>-->
                    <div class="layui-form-item">
                        <label class="layui-form-label" >精品素材视频</label>
                        <div class="layui-input-block" style="display: flex;flex-direction: row;align-items: center;">
                            <input type="text" name="heigh_material_path" placeholder="同步素材路径"  class="layui-input" style="width: 81%;margin-right: 10px;">
                            <a type="button" class="layui-btn layui-btn-normal layui-btn-radius" id="sysnc_heigh_material_video">
                                <i class="layui-icon">&#xe624;</i>同步云素材
                            </a>
                            <tip style="font-size: 12px;color: red;">多个路径,用英文逗号分开</tip>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" >精品视频数: <span class="video_heigh_nums">0</span></label>
                        <div class="layui-input-block" style="display: flex;flex-direction: row;align-items: center;">
                            <div class="video_html_append"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" >普通素材视频</label>
                        <div class="layui-input-block" style="display: flex;flex-direction: row;align-items: center;">
                            <input type="text" name="normal_material_path" placeholder="同步素材路径"  class="layui-input" style="width: 81%;margin-right: 10px;">
                            <a type="button" class="layui-btn layui-btn-normal layui-btn-radius" id="sysnc_normal_material_video">
                                <i class="layui-icon">&#xe624;</i>同步云素材
                            </a>
                            <tip style="font-size: 12px;color: red;">多个路径,用英文逗号分开</tip>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" >普通视频数: <span class="video_normal_nums">0</span></label>
                        <div class="layui-input-block" style="display: flex;flex-direction: row;align-items: center;">
                            <div class="video_html_append2"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>
            let upload_videos_heigh = [];
            let upload_videos_normal = [];

            // 相关接口
            const INSERT_API = "/app/admin/material/index/insert";
            const SYSNC_MATERIAL_API = "/app/admin/material/index/sysncmaterial";

            // 字段 图片地址 icon
            layui.use(["upload", "layer"], function() {
                let input = layui.$("#image").prev();
                input.prev().attr("src", input.val());
                layui.$("#attachment-choose-image_path").on("click", function() {
                    parent.layer.open({
                        type: 2,
                        title: "选择附件",
                        content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                        area: ["95%", "90%"],
                        success: function (layero, index) {
                            parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                input.val(data.url).prev().attr("src", data.url);
                            });
                        }
                    });
                });

                layui.upload.render({
                    elem: "#image",
                    value: "",
                    acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                    url: "/app/admin/upload/image",
                    field: "__file__",
                    headers: get_header({}),
                    done: function (res) {
                        if (res.code > 0) return layui.layer.msg(res.msg);
                        this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                    }
                });
            });

            // 字段  catid
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/material/category/select?format=select&limit=1000",
                    dataType: "json",
                    async:true,
                    headers: get_header({"format":"select"}),
                    success: function (res) {
                        layui.xmSelect.render({
                            el: "#catid",
                            name: "catid",
                            filterable: true,
                            data: res.data,
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                        })
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            layui.use(['form','element','upload'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    $ = layui.$;
                upload = layui.upload;

                batch_upload_video(upload, 'video_upload', batch_upload_video_html);

                layui.$('#sysnc_heigh_material_video').click(function () {
                   let material_path = layui.$('input[name="heigh_material_path"]').val();
                   if(material_path==false){
                       layer.msg('素材路径必填',{icon:2,time:1000});
                   }
                    let param = {};
                   param.material_path = material_path;
                    let loading = layer.load();
                    layui.$.ajax({
                        url: SYSNC_MATERIAL_API,
                        type: "POST",
                        dateType: "json",
                        data: param,
                        async:true,
                        headers: get_header(param),
                        success: function (res) {
                            layer.close(loading);
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                upload_videos_heigh = [];
                                $.each(res.data['videos'], function (i,v) {
                                    upload_videos_heigh.push({"video":v.url,"name":v.name});
                                })

                                reference_render();
                            });
                        }
                    });
                })

                layui.$('#sysnc_normal_material_video').click(function () {
                   let material_path = layui.$('input[name="normal_material_path"]').val();
                   if(material_path==false){
                       layer.msg('素材路径必填',{icon:2,time:1000});
                   }
                    let param = {};
                   param.material_path = material_path;
                    let loading = layer.load();
                    layui.$.ajax({
                        url: SYSNC_MATERIAL_API,
                        type: "POST",
                        dateType: "json",
                        data: param,
                        async:true,
                        headers: get_header(param),
                        success: function (res) {
                            layer.close(loading);
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                upload_videos_normal = [];
                                $.each(res.data['videos'], function (i,v) {
                                    upload_videos_normal.push({"video":v.url,"name":v.name});
                                })

                                reference_render2();
                            });
                        }
                    });
                })
            });

            //提交事件
            layui.use(["form", "popup"], function () {

                layui.form.on("submit(save)", function (data) {
                    data.field['orig_heigh_video_data'] = upload_videos_heigh;
                    data.field['orig_normal_video_data'] = upload_videos_normal;
                    let loading = layer.load();
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        async:true,
                        headers: get_header(data.field),
                        success: function (res) {
                            layer.close(loading);
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

            //批量上传成功后，回调函数
            function batch_upload_video_html(images,elem) {
                //console.log(images);
                $.each(images, function (i,v) {
                    upload_videos.push({"video":v.url,"name":v.name});
                })

                reference_render();
            }

            function delete_reference($this) {
                var name = $($this).data('name');

                $.each(upload_videos, function (i,v) {
                    if(v && name==v.name){
                        upload_videos.splice(i,1);
                    }
                })

                reference_render();
            }

            function reference_render() {
                //console.log(upload_images);
                if(upload_videos_heigh.length==0){
                    $('.video_html_append').html('');
                    return false;
                }

                var upload_video_temp = [];
                var html = '';
                html += ' <fieldset class="table-search-fieldset" style="margin-top: 10px;padding-bottom:25px;border: 1px solid #e6e6e6;color: #6b6b6b;">\n';
                html += '   <div class="wrapper" style="display: flex;flex-direction:row;flex-wrap: wrap;">\n';
                $.each(upload_videos_heigh, function (i,v) {
                    if(i<8){
                        // html +=  '    <div class="app_icon_parent" style="padding:5px;width: 155px;height: 175px;"  draggable="true"\n' +
                        //     '            ondragstart="onDragStart(event)"\n' +
                        //     '            ondragover="onDragOver(event)"\n' +
                        //     '            ondrop="onDrop(event,'+upload_images_draggable+')">\n';
                        html += '<div style="padding:5px;width: 155px;height: 175px;">';

                        html += '          <div style="width: 150px;">';
                        html += '               <a href="javascript:;" data-video="'+v.video+'" data-name="'+v.name+'" onclick="open_video(this)"><video width="120" height="150"><source src="'+v.video+'" type="video/mp4"><object data="'+v.video+'" width="500" height="800"></object> </video></a>';
                        //html += '               <i class="layui-icon layui-icon-delete btn-delete-preview-image" data-name="'+v.name+'" onclick="delete_reference(this);event.cancelBubble =true;" style="color: red; font-size: 25px;position: relative;top: -130px;left: -10px;"></i>';
                        html += '           </div>';
                        html += '           <div style="margin-top: 1px;">'+v.name+'</div>';
                        html += '          \n';
                        html +=    '  </div>\n';

                    }

                    upload_video_temp.push({video:v.video, name:v.name});
                })

                html +='    </div>';
                html +='</fieldset>';

                $('.video_html_append').html(html);
                $('.video_heigh_nums').html(upload_video_temp.length);

                upload_videos_heigh = upload_video_temp;
            }

            function reference_render2() {
                //console.log(upload_images);
                if(upload_videos_normal.length==0){
                    $('.video_html_append2').html('');
                    return false;
                }

                var upload_video_temp = [];
                var html = '';
                html += ' <fieldset class="table-search-fieldset" style="margin-top: 10px;padding-bottom:25px;border: 1px solid #e6e6e6;color: #6b6b6b;">\n';
                html += '   <div class="wrapper" style="display: flex;flex-direction:row;flex-wrap: wrap;">\n';
                $.each(upload_videos_normal, function (i,v) {
                    if(i<11){
                        // html +=  '    <div class="app_icon_parent" style="padding:5px;width: 155px;height: 175px;"  draggable="true"\n' +
                        //     '            ondragstart="onDragStart(event)"\n' +
                        //     '            ondragover="onDragOver(event)"\n' +
                        //     '            ondrop="onDrop(event,'+upload_images_draggable+')">\n';
                        html += '<div style="padding:5px;width: 155px;height: 175px;">';

                        html += '          <div style="width: 150px;">';
                        html += '               <a href="javascript:;" data-video="'+v.video+'" data-name="'+v.name+'" onclick="open_video(this)"><video width="120" height="150"><source src="'+v.video+'" type="video/mp4"><object data="'+v.video+'" width="500" height="800"></object> </video></a>';
                        //html += '               <i class="layui-icon layui-icon-delete btn-delete-preview-image" data-name="'+v.name+'" onclick="delete_reference(this);event.cancelBubble =true;" style="color: red; font-size: 25px;position: relative;top: -130px;left: -10px;"></i>';
                        html += '           </div>';
                        html += '           <div style="margin-top: 1px;">'+v.name+'</div>';
                        html += '          \n';
                        html +=    '  </div>\n';

                    }

                    upload_video_temp.push({video:v.video, name:v.name});
                })

                html +='    </div>';
                html +='</fieldset>';

                $('.video_html_append2').html(html);
                $('.video_normal_nums').html(upload_video_temp.length);

                upload_videos_normal = upload_video_temp;
            }

            //拖动后，回调函数，重新组合数据
            function upload_images_draggable() {
                upload_videos = [];
                $('.wrapper').find('.app_icon_parent').each(function (i,v) {
                    upload_videos.push({video:$(v).find('a').data('video'),name:$(v).find('a').data('name')});
                })

                reference_render();
            }
        </script>

    </body>
</html>
